package StudentDao;

import Student.Student;

public class StudentDao {
    //定义一个学生类数组
    private static Student[] stus = new Student[5];
    public boolean addStudent(Student stu) {
        //定义一个index判断数组是否为空
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if(student == null)
            {
                index = i;
                break;
            }
        }
        //根据index值判断是否添加成功
        if(index == -1)
        {
            return false;
        }
        else{
            stus[index] = stu;
            return true;
        }
    }

    public Student[] fullStudent() {
        return stus;
    }
//删除对应学号操作
    public void deleteStudentById(String delId) {
        //找到delId学生对应位置的index
        int index = getIndex(stus,delId);
        //将index位置置空
        stus[index]=null;
    }
//找到所需要的学号的位置
    private int getIndex(Student[] stus, String delId) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu!=null && stu.getId().equals(delId)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newstu) {
        //使用getIndex函数找到目标位置
        int index = getIndex(stus,updateId);
        //进行替换
        stus[index] =newstu;
    }
}
